﻿#include "login.h"
#include <qboxlayout.h>
#include <qtextedit.h>
#include <qlabel.h>
#include <qpushbutton.h>
#include <qdebug.h>
#include <qmessagebox.h>
#include <QtSql/qsqldatabase.h> //sql驱动基础
#include <QtSql/QSqlQuery>//sql查询相关
#include <QtSql/QSqlError>//sql输出错误


login::login(QWidget*)
{
    setupUi();
}

void login::setupUi()
{
    QWidget* mainWidget = new QWidget(this);
    setCentralWidget(mainWidget);
    QVBoxLayout* mainLayout = new QVBoxLayout(mainWidget);

//    QHBoxLayout* hostLayout = new QHBoxLayout;
//    host = new QTextEdit;
//    host->setText("server.natappfree.cc");
//    host->setMaximumHeight(30);
//    QLabel* hostLabel = new QLabel(tr("主机"));
//    hostLayout->addWidget(hostLabel);
//    hostLayout->addWidget(host);
//    mainLayout->addLayout(hostLayout);

//    QHBoxLayout* portLayout = new QHBoxLayout;
//    port = new QTextEdit;
//    port->setMaximumHeight(30);
//    QLabel* portLabel = new QLabel(tr("端口"));
//    portLayout->addWidget(portLabel);
//    portLayout->addWidget(port);
//    mainLayout->addLayout(portLayout);

    QHBoxLayout* userLayout = new QHBoxLayout;
    user = new QTextEdit;
    user->setMaximumHeight(30);
    QLabel* userLabel = new QLabel(u8"账号");
    userLayout->addWidget(userLabel);
    userLayout->addWidget(user);
    mainLayout->addLayout(userLayout);

    QHBoxLayout* passwordLayout = new QHBoxLayout;
    password = new QTextEdit;
    password->setMaximumHeight(30);
    QLabel* passwordLabel = new QLabel(u8"密码");
    passwordLayout->addWidget(passwordLabel);
    passwordLayout->addWidget(password);
    mainLayout->addLayout(passwordLayout);

    QHBoxLayout* buttonLayout = new QHBoxLayout;
    loginButton = new QPushButton(u8"登录");
    cancelButton = new QPushButton(u8"取消");

    connect(loginButton, SIGNAL(clicked()), this, SLOT(loginSlot()));
    //connect(cancelButton, SIGNAL(clicked()), this, SLOT(login::close()));
    buttonLayout->addWidget(loginButton);
    buttonLayout->addWidget(cancelButton);
    mainLayout->addLayout(buttonLayout);

    xray_predict = new xray_predict_mainwindow;
    //connect(xray_predict,SIGNAL(insertSignal(QStringList&)),this,SLOT(insertSlot(QStringList&)));
    //connect(xray_predict,SIGNAL(selectSignal(QString&)),this,SLOT(selectSlot(QString&)));
}

void login::loginSlot()
{
    if(user->toPlainText().isEmpty()||password->toPlainText().isEmpty())
    {
        QMessageBox::warning(this,"warning",u8"请输入账号密码",QMessageBox::Ok);
        return;
    }
    //qDebug() << QSqlDatabase::drivers();
    //主要方法
    //QSqlDatabase data_base = QSqlDatabase::addDatabase("QMYSQL");//添加驱动
    //data_base.setHostName(host->toPlainText());  //设置主机地址
    //data_base.setPort(port->toPlainText().toInt());  //mysql设置端口
    //data_base.setDatabaseName("pneumonia - detector");  //设置数据库名称
    //data_base.setUserName(user->toPlainText());  //设置用户名
    //data_base.setPassword(password->toPlainText());  //设置密码
    //if (!data_base.open())//打开数据库
    //{
    //	qDebug() << "connect failed";
    //	qDebug() << data_base.lastError();//.databaseText()  输出错误信息
    //}
    //else
    //	qDebug() << "success";
    //data_base.close();//关闭数据库
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
        db.setHostName("127.0.0.1");
        db.setPort(3306);
        db.setDatabaseName("1");
        db.setUserName("root");
        db.setPassword("admin");
        bool ok = db.open();
        if (ok){
            //QMessageBox::information(this, "infor", "success");
            QString sql = QString("select * from doctor_xray where username=\"").append(user->toPlainText()) + "\"";

            QSqlQuery result = db.exec(sql);
                 while(result.next()){
                    if(password->toPlainText() == result.value("password"))
                    {
                        xray_predict->currentId = result.value("id").toString();
                        this->hide();
                        xray_predict->show();
                        return;
                    }
                 }
            QMessageBox::warning(this,"error",u8"账号密码错误",QMessageBox::Ok);


        }
        else {
            QMessageBox::information(this, "infor", "open failed");
            qDebug()<<"error open database because"<<db.lastError().text();
        }


}
